package com.insurance.modules.claim.controller;

import com.alibaba.fastjson.JSONObject;
import com.insurance.common.common.ResponseData;
import com.insurance.common.utils.R;
import com.insurance.datasource.annotation.DataSource;
import com.insurance.modules.claim.entity.ClaimCaseEntity;
import com.insurance.modules.claim.entity.ClaimSettlementEntity;
import com.insurance.modules.claim.entity.PolicyInfoEntity;
import com.insurance.modules.claim.service.ClaimCaseService;
import com.insurance.modules.claim.service.ClaimSettlementService;
import com.insurance.modules.claim.service.PolicyInfoService;
import com.insurance.modules.claim.service.SettlementAmountService;
import com.insurance.modules.claim.vo.request.SettlementAmountSaveReq;
import com.insurance.modules.claim.vo.response.SettlementAmountRsp;
import com.insurance.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


/**
 * 理赔金额表
 *
 * @author weiruifeng
 * @email
 * @date 2021-09-24 17:42:47
 */
@RestController
@RequestMapping("claim/settlementamount")
@Api(tags = "理赔金额")
public class SettlementAmountController extends AbstractController {
    @Autowired
    private SettlementAmountService settlementAmountService;
    @Autowired
    private ClaimCaseService claimCaseService;
    @Autowired
    private ClaimSettlementService claimSettlementService;
    @Autowired
    private PolicyInfoService policyInfoService;
    /**
     * 根据案件理赔表id查询结案金额等信息
     */
    @PostMapping("/info")
    //@RequiresPermissions("claim:settlementamount:info")
    @ApiOperation(value = "查询信息", notes = "根据id查询信息")
    public ResponseData<JSONObject> info(@RequestParam(value = "csId") String csId, @RequestParam(value = "caseId") String caseId) {
        SettlementAmountRsp settlementAmount = settlementAmountService.getByCsId(csId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("settlementAmount", settlementAmount);
        jsonObject.put("outsourcingFlag", 0);
        //获取是否是外包
        ClaimCaseEntity claimCase = claimCaseService.getById(caseId);
        PolicyInfoEntity policyInfo = policyInfoService.getById(claimCase.getPolicyId());
        Integer outsourcingFlag = policyInfo.getOutsourcingFlag();
        jsonObject.put("outsourcingFlag", outsourcingFlag);
        return ResponseData.success(jsonObject);
    }

    /**
     * 保存结案金额等信息
     */
    @PostMapping("/save")
    //@RequiresPermissions("claim:settlementamount:save")
    @ApiOperation(value = "保存结案金额等信息", notes = "保存结案金额等信息")
    public R save(@RequestBody SettlementAmountSaveReq req) {
        Date date = new Date();
        if(null == req.getId()){
            req.setCreateBy(getUserId());
            req.setCreateTime(date);
        }
        req.setUpdateBy(getUserId());
        req.setUpdateTime(date);
        settlementAmountService.saveSettlementAmountAndFile(req);
        return R.ok();
    }

    /**
     * 结案
     */
    @PostMapping("/finish")
    //@RequiresPermissions("claim:settlementamount:finish")
    @ApiOperation(value = "结案", notes = "结案")
    public R finish(@RequestBody SettlementAmountSaveReq req) {
        Date date = new Date();
        if(null == req.getId()){
            req.setCreateBy(getUserId());
            req.setCreateTime(date);
        }
        req.setUpdateBy(getUserId());
        req.setUpdateTime(date);
        settlementAmountService.finishCase(req);
        return R.ok();
    }

    /**
     * 诉讼
     */
    @PostMapping("/litigation")
    //@RequiresPermissions("claim:settlementamount:finish")
    @ApiOperation(value = "诉讼", notes = "诉讼")
    public R litigation(@RequestBody SettlementAmountSaveReq req) {
        settlementAmountService.litigation(req);
        return R.ok();
    }

}
